package cn.yj.common.factory;

import cn.yj.alipay.service.AlipaySign;
import cn.yj.common.emums.PayType;
import cn.yj.common.interfaces.ISign;
import cn.yj.wx.pay.service.WechatPaySign;

import javax.xml.rpc.ServiceException;
import java.util.Map;

/**
 * 支付结果验签
 *
 * @author 永健
 * @since 2022-02-18 11:53
 */
public class SignFactory {

    private PayType payType;

    private ISign iSign;

    private SignFactory(PayType payType) {
        this.payType = payType;
    }

    public static SignFactory build(PayType payType) {
        return new SignFactory(payType);
    }

    public SignFactory selectExecutor() {
        switch (payType) {
            case ALIPAY:
                this.iSign = AlipaySign.getInstance();
                break;
            default:
                this.iSign = WechatPaySign.getInstance();
                break;
        }
        return this;
    }

    /**
     * 支付结果验签
     *
     * @param params 参数
     *
     * @throws ServiceException
     */
    public boolean deSign(Map<String, String> params) {
        if (this.iSign == null) {
            this.selectExecutor();
        }
        return this.iSign.decryptSign(params);
    }

}
